home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / timemodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  20.8 KB  |  873 lines

  1. /* Time module */
  2.  
  3. #include "Python.h"
  4.  
  5. #include <ctype.h>
  6.  
  7. #ifdef HAVE_SELECT
  8. #include "mymath.h"
  9. #endif
  10.  
  11. #ifdef macintosh
  12. #include <time.h>
  13. #include <OSUtils.h>
  14. #ifdef USE_GUSI2
  15. /* GUSI, the I/O library which has the time() function and such uses the
  16. ** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
  17. ** the ANSI epoch of 1900.
  18. */
  19. #define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
  20. #endif /* USE_GUSI2 */
  21. #else
  22. #include <sys/types.h>
  23. #endif
  24.  
  25. #ifdef _AMIGA
  26. #include <proto/dos.h>
  27. #endif
  28.  
  29. #ifdef QUICKWIN
  30. #include <io.h>
  31. #endif
  32.  
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36.  
  37. #if defined(HAVE_SELECT) && !defined(__BEOS__)
  38. #include "myselect.h"
  39. #else
  40. #include "mytime.h"
  41. #endif
  42.  
  43. #ifdef HAVE_FTIME
  44. #include <sys/timeb.h>
  45. #if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
  46. extern int ftime();
  47. #endif /* MS_WINDOWS */
  48. #endif /* HAVE_FTIME */
  49.  
  50. #if defined(__WATCOMC__) && !defined(__QNX__)
  51. #include <i86.h>
  52. #else
  53. #ifdef MS_WINDOWS
  54. #include <windows.h>
  55. #ifdef MS_WIN16
  56. /* These overrides not needed for Win32 */
  57. #define timezone _timezone
  58. #define tzname _tzname
  59. #define daylight _daylight
  60. #define altzone _altzone
  61. #endif /* MS_WIN16 */
  62. #endif /* MS_WINDOWS */
  63. #endif /* !__WATCOMC__ || __QNX__ */
  64.  
  65. #ifdef MS_WIN32
  66. /* Win32 has better clock replacement */
  67. #include <largeint.h>
  68. #undef HAVE_CLOCK /* We have our own version down below */
  69. #endif /* MS_WIN32 */
  70.  
  71. #if defined(PYCC_VACPP)
  72. #include <sys/time.h>
  73. #endif
  74.  
  75. #ifdef __BEOS__
  76. /* For bigtime_t, snooze(). - [cjh] */
  77. #include <support/SupportDefs.h>
  78. #include <kernel/OS.h>
  79. #ifndef CLOCKS_PER_SEC
  80. /* C'mon, fix the bloody headers... - [cjh] */
  81. #define CLOCKS_PER_SEC 1000
  82. #endif
  83. #endif
  84.  
  85. /* Forward declarations */
  86. #include "protos/timemodule.h"
  87. static int floatsleep Py_PROTO((double));
  88. static double floattime Py_PROTO((void));
  89.  
  90. /* For Y2K check */
  91. static PyObject *moddict;
  92.  
  93. #ifdef macintosh
  94. /* Our own timezone. We have enough information to deduce whether
  95. ** DST is on currently, but unfortunately we cannot put it to good
  96. ** use because we don't know the rules (and that is needed to have
  97. ** localtime() return correct tm_isdst values for times other than
  98. ** the current time. So, we cop out and only tell the user the current
  99. ** timezone.
  100. */
  101. static long timezone;
  102.  
  103. static void 
  104. initmactimezone()
  105. {
  106.     MachineLocation    loc;
  107.     long        delta;
  108.  
  109.     ReadLocation(&loc);
  110.     
  111.     if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
  112.         return;
  113.     
  114.     delta = loc.u.gmtDelta & 0x00FFFFFF;
  115.     
  116.     if (delta & 0x00800000)
  117.         delta |= 0xFF000000;
  118.     
  119.     timezone = -delta;
  120. }
  121. #endif /* macintosh */
  122.  
  123.  
  124. static PyObject *
  125. time_time(self, args)
  126.     PyObject *self;
  127.     PyObject *args;
  128. {
  129.     double secs;
  130.     if (!PyArg_NoArgs(args))
  131.         return NULL;
  132.     secs = floattime();
  133.     if (secs == 0.0) {
  134.         PyErr_SetFromErrno(PyExc_IOError);
  135.         return NULL;
  136.     }
  137.     return PyFloat_FromDouble(secs);
  138. }
  139.  
  140. static char time_doc[] =
  141. "time() -> floating point number\n\
  142. \n\
  143. Return the current time in seconds since the Epoch.\n\
  144. Fractions of a second may be present if the system clock provides them.";
  145.  
  146. #ifdef HAVE_CLOCK
  147.  
  148. #ifndef CLOCKS_PER_SEC
  149. #ifdef CLK_TCK
  150. #define CLOCKS_PER_SEC CLK_TCK
  151. #else
  152. #define CLOCKS_PER_SEC 1000000
  153. #endif
  154. #endif
  155.  
  156. static PyObject *
  157. time_clock(self, args)
  158.     PyObject *self;
  159.     PyObject *args;
  160. {
  161.     if (!PyArg_NoArgs(args))
  162.         return NULL;
  163.     return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
  164. }
  165. #endif /* HAVE_CLOCK */
  166.  
  167. #ifdef MS_WIN32
  168. /* Due to Mark Hammond */
  169. static PyObject *
  170. time_clock(self, args)
  171.     PyObject *self;
  172.     PyObject *args;
  173. {
  174.     static LARGE_INTEGER ctrStart;
  175.     static LARGE_INTEGER divisor = {0,0};
  176.     LARGE_INTEGER now, diff, rem;
  177.  
  178.     if (!PyArg_NoArgs(args))
  179.         return NULL;
  180.  
  181.     if (LargeIntegerEqualToZero(divisor)) {
  182.         QueryPerformanceCounter(&ctrStart);
  183.         if (!QueryPerformanceFrequency(&divisor) || 
  184.             LargeIntegerEqualToZero(divisor)) {
  185.                 /* Unlikely to happen - 
  186.                    this works on all intel machines at least! 
  187.                    Revert to clock() */
  188.             return PyFloat_FromDouble(clock());
  189.         }
  190.     }
  191.     QueryPerformanceCounter(&now);
  192.     diff = LargeIntegerSubtract(now, ctrStart);
  193.     diff = LargeIntegerDivide(diff, divisor, &rem);
  194.     /* XXX - we assume both divide results fit in 32 bits.  This is
  195.        true on Intels.  First person who can afford a machine that 
  196.        doesnt deserves to fix it :-)
  197.     */
  198.     return PyFloat_FromDouble((double)diff.LowPart + 
  199.                       ((double)rem.LowPart / (double)divisor.LowPart));
  200. }
  201.  
  202. #define HAVE_CLOCK /* So it gets included in the methods */
  203. #endif /* MS_WIN32 */
  204.  
  205. #ifdef HAVE_CLOCK
  206. static char clock_doc[] =
  207. "clock() -> floating point number\n\
  208. \n\
  209. Return the CPU time or real time since the start of the process or since\n\
  210. the first call to clock().  This has as much precision as the system records.";
  211. #endif
  212.  
  213. static PyObject *
  214. time_sleep(self, args)
  215.     PyObject *self;
  216.     PyObject *args;
  217. {
  218.     double secs;
  219.     if (!PyArg_Parse(args, "d", &secs))
  220.         return NULL;
  221.     if (floatsleep(secs) != 0)
  222.         return NULL;
  223.     Py_INCREF(Py_None);
  224.     return Py_None;
  225. }
  226.  
  227. static char sleep_doc[] =
  228. "sleep(seconds)\n\
  229. \n\
  230. Delay execution for a given number of seconds.  The argument may be\n\
  231. a floating point number for subsecond precision.";
  232.  
  233. static PyObject *
  234. tmtotuple(p)
  235.     struct tm *p;
  236. {
  237.     return Py_BuildValue("(iiiiiiiii)",
  238.                  p->tm_year + 1900,
  239.                  p->tm_mon + 1,       /* Want January == 1 */
  240.                  p->tm_mday,
  241.                  p->tm_hour,
  242.                  p->tm_min,
  243.                  p->tm_sec,
  244.                  (p->tm_wday + 6) % 7, /* Want Monday == 0 */
  245.                  p->tm_yday + 1,       /* Want January, 1 == 1 */
  246.                  p->tm_isdst);
  247. }
  248.  
  249. static PyObject *
  250. time_convert(when, function)
  251.     time_t when;
  252.     struct tm * (*function) Py_PROTO((const time_t *));
  253. {
  254.     struct tm *p;
  255.     errno = 0;
  256. #if defined(macintosh) && defined(USE_GUSI2)
  257.     when = when + GUSI_TO_MSL_EPOCH;
  258. #endif
  259.     p = function(&when);
  260.     if (p == NULL) {
  261. #ifdef EINVAL
  262.         if (errno == 0)
  263.             errno = EINVAL;
  264. #endif
  265.         return PyErr_SetFromErrno(PyExc_IOError);
  266.     }
  267.     return tmtotuple(p);
  268. }
  269.  
  270. static PyObject *
  271. time_gmtime(self, args)
  272.     PyObject *self;
  273.     PyObject *args;
  274. {
  275.     double when;
  276.     if (!PyArg_Parse(args, "d", &when))
  277.         return NULL;
  278.     return time_convert((time_t)when, gmtime);
  279. }
  280.  
  281. static char gmtime_doc[] =
  282. "gmtime(seconds) -> tuple\n\
  283. \n\
  284. Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
  285.  
  286. static PyObject *
  287. time_localtime(self, args)
  288.     PyObject *self;
  289.     PyObject *args;
  290. {
  291.     double when;
  292.     if (!PyArg_Parse(args, "d", &when))
  293.         return NULL;
  294.     return time_convert((time_t)when, localtime);
  295. }
  296.  
  297. static char localtime_doc[] =
  298. "localtime(seconds) -> tuple\n\
  299. Convert seconds since the Epoch to a time tuple expressing local time.";
  300.  
  301. static int
  302. gettmarg(args, p)
  303.     PyObject *args;
  304.     struct tm *p;
  305. {
  306.     int y;
  307.     memset((ANY *) p, '\0', sizeof(struct tm));
  308.  
  309.     if (!PyArg_Parse(args, "(iiiiiiiii)",
  310.              &y,
  311.              &p->tm_mon,
  312.              &p->tm_mday,
  313.              &p->tm_hour,
  314.              &p->tm_min,
  315.              &p->tm_sec,
  316.              &p->tm_wday,
  317.              &p->tm_yday,
  318.              &p->tm_isdst))
  319.         return 0;
  320.     if (y < 1900) {
  321.         PyObject *accept = PyDict_GetItemString(moddict,
  322.                             "accept2dyear");
  323.         if (accept == NULL || !PyInt_Check(accept) ||
  324.             PyInt_AsLong(accept) == 0) {
  325.             PyErr_SetString(PyExc_ValueError,
  326.                     "year >= 1900 required");
  327.             return 0;
  328.         }
  329.         if (69 <= y && y <= 99)
  330.             y += 1900;
  331.         else if (0 <= y && y <= 68)
  332.             y += 2000;
  333.         else {
  334.             PyErr_SetString(PyExc_ValueError,
  335.                     "year out of range (00-99, 1900-*)");
  336.             return 0;
  337.         }
  338.     }
  339.     p->tm_year = y - 1900;
  340.     p->tm_mon--;
  341.     p->tm_wday = (p->tm_wday + 1) % 7;
  342.     p->tm_yday--;
  343.     return 1;
  344. }
  345.  
  346. #ifdef HAVE_STRFTIME
  347. static PyObject *
  348. time_strftime(self, args)
  349.     PyObject *self;
  350.     PyObject *args;
  351. {
  352.     PyObject *tup;
  353.     struct tm buf;
  354.     const char *fmt;
  355.     int fmtlen, buflen;
  356.     char *outbuf = 0;
  357.     int i;
  358.  
  359.     memset((ANY *) &buf, '\0', sizeof(buf));
  360.  
  361.     if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup) 
  362.         || !gettmarg(tup, &buf))
  363.         return NULL;
  364.     fmtlen = strlen(fmt);
  365.  
  366.     /* I hate these functions that presume you know how big the output
  367.      * will be ahead of time...
  368.      */
  369.     for (i = 1024; ; i += i) {
  370.         outbuf = malloc(i);
  371.         if (outbuf == NULL) {
  372.             return PyErr_NoMemory();
  373.         }
  374.         buflen = strftime(outbuf, i, fmt, &buf);
  375.         if (buflen > 0 || i >= 256 * fmtlen) {
  376.             /* If the buffer is 256 times as long as the format,
  377.                it's probably not failing for lack of room!
  378.                More likely, the format yields an empty result,
  379.                e.g. an empty format, or %Z when the timezone
  380.                is unknown. */
  381.             PyObject *ret;
  382.             ret = PyString_FromStringAndSize(outbuf, buflen);
  383.             free(outbuf);
  384.             return ret;
  385.         }
  386.         free(outbuf);
  387.     }
  388. }
  389.  
  390. static char strftime_doc[] =
  391. "strftime(format, tuple) -> string\n\
  392. \n\
  393. Convert a time tuple to a string according to a format specification.\n\
  394. See the library reference manual for formatting codes.";
  395. #endif /* HAVE_STRFTIME */
  396.  
  397. #ifdef HAVE_STRPTIME
  398.  
  399. #if 0
  400. extern char *strptime(); /* Enable this if it's not declared in <time.h> */
  401. #endif
  402.  
  403. static PyObject *
  404. time_strptime(self, args)
  405.     PyObject *self;
  406.     PyObject *args;
  407. {
  408.     struct tm tm;
  409.     char *fmt = "%a %b %d %H:%M:%S %Y";
  410.     char *buf;
  411.     char *s;
  412.  
  413.     if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
  414.             return NULL;
  415.     memset((ANY *) &tm, '\0', sizeof(tm));
  416.     s = strptime(buf, fmt, &tm);
  417.     if (s == NULL) {
  418.         PyErr_SetString(PyExc_ValueError, "format mismatch");
  419.         return NULL;
  420.     }
  421.     while (*s && isspace(*s))
  422.         s++;
  423.     if (*s) {
  424.         PyErr_Format(PyExc_ValueError,
  425.                  "unconverted data remains: '%.400s'", s);
  426.         return NULL;
  427.     }
  428.     return tmtotuple(&tm);
  429. }
  430.  
  431. static char strptime_doc[] =
  432. "strptime(string, format) -> tuple\n\
  433. Parse a string to a time tuple according to a format specification.\n\
  434. See the library reference manual for formatting codes (same as strftime()).";
  435. #endif /* HAVE_STRPTIME */
  436.  
  437. static PyObject *
  438. time_asctime(self, args)
  439.     PyObject *self;
  440.     PyObject *args;
  441. {
  442.     PyObject *tup;
  443.     struct tm buf;
  444.     char *p;
  445.     if (!PyArg_ParseTuple(args, "O:asctime", &tup))
  446.         return NULL;
  447.     if (!gettmarg(tup, &buf))
  448.         return NULL;
  449.     p = asctime(&buf);
  450.     if (p[24] == '\n')
  451.         p[24] = '\0';
  452.     return PyString_FromString(p);
  453. }
  454.  
  455. static char asctime_doc[] =
  456. "asctime(tuple) -> string\n\
  457. \n\
  458. Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
  459.  
  460. static PyObject *
  461. time_ctime(self, args)
  462.     PyObject *self;
  463.     PyObject *args;
  464. {
  465.     double dt;
  466.     time_t tt;
  467.     char *p;
  468.     if (!PyArg_Parse(args, "d", &dt))
  469.         return NULL;
  470.     tt = (time_t)dt;
  471. #if defined(macintosh) && defined(USE_GUSI2)
  472.     tt = tt + GUSI_TO_MSL_EPOCH;
  473. #endif
  474.     p = ctime(&tt);
  475.     if (p == NULL) {
  476.         PyErr_SetString(PyExc_ValueError, "unconvertible time");
  477.         return NULL;
  478.     }
  479.     if (p[24] == '\n')
  480.         p[24] = '\0';
  481.     return PyString_FromString(p);
  482. }
  483.  
  484. static char ctime_doc[] =
  485. "ctime(seconds) -> string\n\
  486. \n\
  487. Convert a time in seconds since the Epoch to a string in local time.\n\
  488. This is equivalent to asctime(localtime(seconds)).";
  489.  
  490. #ifdef HAVE_MKTIME
  491. static PyObject *
  492. time_mktime(self, args)
  493.     PyObject *self;
  494.     PyObject *args;
  495. {
  496.     PyObject *tup;
  497.     struct tm buf;
  498.     time_t tt;
  499.     if (!PyArg_ParseTuple(args, "O:mktime", &tup))
  500.         return NULL;
  501.     tt = time(&tt);
  502.     buf = *localtime(&tt);
  503.     if (!gettmarg(tup, &buf))
  504.         return NULL;
  505.     tt = mktime(&buf);
  506.     if (tt == (time_t)(-1)) {
  507.         PyErr_SetString(PyExc_OverflowError,
  508.                                 "mktime argument out of range");
  509.         return NULL;
  510.     }
  511. #if defined(macintosh) && defined(USE_GUSI2)
  512.     tt = tt - GUSI_TO_MSL_EPOCH;
  513. #endif
  514.     return PyFloat_FromDouble((double)tt);
  515. }
  516.  
  517. static char mktime_doc[] =
  518. "mktime(tuple) -> floating point number\n\
  519. \n\
  520. Convert a time tuple in local time to seconds since the Epoch.";
  521. #endif /* HAVE_MKTIME */
  522.  
  523. static PyMethodDef time_methods[] = {
  524.     {"time",    time_time, 0, time_doc},
  525. #ifdef HAVE_CLOCK
  526.     {"clock",    time_clock, 0, clock_doc},
  527. #endif
  528.     {"sleep",    time_sleep, 0, sleep_doc},
  529.     {"gmtime",    time_gmtime, 0, gmtime_doc},
  530.     {"localtime",    time_localtime, 0, localtime_doc},
  531.     {"asctime",    time_asctime, 1, asctime_doc},
  532.     {"ctime",    time_ctime, 0, ctime_doc},
  533. #ifdef HAVE_MKTIME
  534.     {"mktime",    time_mktime, 1, mktime_doc},
  535. #endif
  536. #ifdef HAVE_STRFTIME
  537.     {"strftime",    time_strftime, 1, strftime_doc},
  538. #endif
  539. #ifdef HAVE_STRPTIME
  540.     {"strptime",    time_strptime, 1, strptime_doc},
  541. #endif
  542.     {NULL,        NULL}        /* sentinel */
  543. };
  544.  
  545. static void
  546. ins(d, name, v)
  547.     PyObject *d;
  548.     char *name;
  549.     PyObject *v;
  550. {
  551.     if (v == NULL)
  552.         Py_FatalError("Can't initialize time module -- NULL value");
  553.     if (PyDict_SetItemString(d, name, v) != 0)
  554.         Py_FatalError(
  555.         "Can't initialize time module -- PyDict_SetItemString failed");
  556.     Py_DECREF(v);
  557. }
  558.  
  559. static char module_doc[] =
  560. "This module provides various functions to manipulate time values.\n\
  561. \n\
  562. There are two standard representations of time.  One is the number\n\
  563. of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer\n\
  564. or a floating point number (to represent fractions of seconds).\n\
  565. The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
  566. The actual value can be retrieved by calling gmtime(0).\n\
  567. \n\
  568. The other representation is a tuple of 9 integers giving local time.\n\
  569. The tuple items are:\n\
  570.   year (four digits, e.g. 1998)\n\
  571.   month (1-12)\n\
  572.   day (1-31)\n\
  573.   hours (0-23)\n\
  574.   minutes (0-59)\n\
  575.   seconds (0-59)\n\
  576.   weekday (0-6, Monday is 0)\n\
  577.   Julian day (day in the year, 1-366)\n\
  578.   DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
  579. If the DST flag is 0, the time is given in the regular time zone;\n\
  580. if it is 1, the time is given in the DST time zone;\n\
  581. if it is -1, mktime() should guess based on the date and time.\n\
  582. \n\
  583. Variables:\n\
  584. \n\
  585. timezone -- difference in seconds between UTC and local standard time\n\
  586. altzone -- difference in  seconds between UTC and local DST time\n\
  587. daylight -- whether local time should reflect DST\n\
  588. tzname -- tuple of (standard time zone name, DST time zone name)\n\
  589. \n\
  590. Functions:\n\
  591. \n\
  592. time() -- return current time in seconds since the Epoch as a float\n\
  593. clock() -- return CPU time since process start as a float\n\
  594. sleep() -- delay for a number of seconds given as a float\n\
  595. gmtime() -- convert seconds since Epoch to UTC tuple\n\
  596. localtime() -- convert seconds since Epoch to local time tuple\n\
  597. asctime() -- convert time tuple to string\n\
  598. ctime() -- convert time in seconds to string\n\
  599. mktime() -- convert local time tuple to seconds since Epoch\n\
  600. strftime() -- convert time tuple to string according to format specification\n\
  601. strptime() -- parse string to time tuple according to format specification\n\
  602. ";
  603.   
  604.  
  605. DL_EXPORT(void)
  606. inittime()
  607. {
  608.     PyObject *m, *d;
  609.     char *p;
  610.     m = Py_InitModule3("time", time_methods, module_doc);
  611.     d = PyModule_GetDict(m);
  612.     /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
  613.     p = getenv("PYTHONY2K");
  614.     ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
  615.     /* Squirrel away the module's dictionary for the y2k check */
  616.     Py_INCREF(d);
  617.     moddict = d;
  618. #if defined(HAVE_TZNAME) && !defined(__GLIBC__)
  619.     tzset();
  620. #ifdef PYOS_OS2
  621.     ins(d, "timezone", PyInt_FromLong((long)_timezone));
  622. #else /* !PYOS_OS2 */
  623.     ins(d, "timezone", PyInt_FromLong((long)timezone));
  624. #endif /* PYOS_OS2 */
  625. #ifdef HAVE_ALTZONE
  626.     ins(d, "altzone", PyInt_FromLong((long)altzone));
  627. #else
  628. #ifdef PYOS_OS2
  629.     ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
  630. #else /* !PYOS_OS2 */
  631.     ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
  632. #endif /* PYOS_OS2 */
  633. #endif
  634.     ins(d, "daylight", PyInt_FromLong((long)daylight));
  635.     ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
  636. #else /* !HAVE_TZNAME || __GLIBC__ */
  637. #ifdef HAVE_TM_ZONE
  638.     {
  639. #define YEAR ((time_t)((365 * 24 + 6) * 3600))
  640.         time_t t;
  641.         struct tm *p;
  642.         long janzone, julyzone;
  643.         char janname[10], julyname[10];
  644.         t = (time((time_t *)0) / YEAR) * YEAR;
  645.         p = localtime(&t);
  646.         janzone = -p->tm_gmtoff;
  647.         strncpy(janname, p->tm_zone ? p->tm_zone : "   ", 9);
  648.         janname[9] = '\0';
  649.         t += YEAR/2;
  650.         p = localtime(&t);
  651.         julyzone = -p->tm_gmtoff;
  652.         strncpy(julyname, p->tm_zone ? p->tm_zone : "   ", 9);
  653.         julyname[9] = '\0';
  654.         
  655.         if( janzone < julyzone ) {
  656.             /* DST is reversed in the southern hemisphere */
  657.             ins(d, "timezone", PyInt_FromLong(julyzone));
  658.             ins(d, "altzone", PyInt_FromLong(janzone));
  659.             ins(d, "daylight",
  660.                 PyInt_FromLong((long)(janzone != julyzone)));
  661.             ins(d, "tzname",
  662.                 Py_BuildValue("(zz)", julyname, janname));
  663.         } else {
  664.             ins(d, "timezone", PyInt_FromLong(janzone));
  665.             ins(d, "altzone", PyInt_FromLong(julyzone));
  666.             ins(d, "daylight",
  667.                 PyInt_FromLong((long)(janzone != julyzone)));
  668.             ins(d, "tzname",
  669.                 Py_BuildValue("(zz)", janname, julyname));
  670.         }
  671.     }
  672. #else
  673. #ifdef macintosh
  674.     /* The only thing we can obtain is the current timezone
  675.     ** (and whether dst is currently _active_, but that is not what
  676.     ** we're looking for:-( )
  677.     */
  678.     initmactimezone();
  679.     ins(d, "timezone", PyInt_FromLong(timezone));
  680.     ins(d, "altzone", PyInt_FromLong(timezone));
  681.     ins(d, "daylight", PyInt_FromLong((long)0));
  682.     ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
  683. #endif /* macintosh */
  684. #endif /* HAVE_TM_ZONE */
  685. #endif /* !HAVE_TZNAME || __GLIBC__ */
  686.     if (PyErr_Occurred())
  687.         Py_FatalError("Can't initialize time module");
  688. }
  689.  
  690.  
  691. /* Implement floattime() for various platforms */
  692.  
  693. static double
  694. floattime()
  695. {
  696.     /* There are three ways to get the time:
  697.       (1) gettimeofday() -- resolution in microseconds
  698.       (2) ftime() -- resolution in milliseconds
  699.       (3) time() -- resolution in seconds
  700.       In all cases the return value is a float in seconds.
  701.       Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  702.       fail, so we fall back on ftime() or time().
  703.       Note: clock resolution does not imply clock accuracy! */
  704. #ifdef HAVE_GETTIMEOFDAY
  705.     {
  706.         struct timeval t;
  707. #ifdef GETTIMEOFDAY_NO_TZ
  708.         if (gettimeofday(&t) == 0)
  709.             return (double)t.tv_sec + t.tv_usec*0.000001;
  710. #else /* !GETTIMEOFDAY_NO_TZ */
  711.         if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  712.             return (double)t.tv_sec + t.tv_usec*0.000001;
  713. #endif /* !GETTIMEOFDAY_NO_TZ */
  714.     }
  715. #endif /* !HAVE_GETTIMEOFDAY */
  716.     {
  717. #if defined(HAVE_FTIME)
  718.         struct timeb t;
  719.         ftime(&t);
  720.         return (double)t.time + (double)t.millitm * (double)0.001;
  721. #else /* !HAVE_FTIME */
  722.         time_t secs;
  723.         time(&secs);
  724.         return (double)secs;
  725. #endif /* !HAVE_FTIME */
  726.     }
  727. }
  728.  
  729.  
  730. /* Implement floatsleep() for various platforms.
  731.    When interrupted (or when another error occurs), return -1 and
  732.    set an exception; else return 0. */
  733.  
  734. static int
  735. #ifdef MPW
  736. floatsleep(double secs)
  737. #else
  738.     floatsleep(secs)
  739.     double secs;
  740. #endif /* MPW */
  741. {
  742. /* XXX Should test for MS_WIN32 first! */
  743. #if defined(HAVE_SELECT) && !defined(__BEOS__)
  744.     struct timeval t;
  745.     double frac;
  746. #if defined (AMITCP) || defined(INET225)
  747.     /* check for availability of an Amiga TCP stack for select() */
  748.     if(!checksocketlib())
  749.     {
  750.         /* no bsdsocket.library-- use dos/Delay() */
  751.         PyErr_Clear();
  752.         Delay((long)(secs*50));        /* XXX Can't interrupt this sleep */
  753.         return 0;
  754.     }
  755. #endif
  756.     frac = fmod(secs, 1.0);
  757.     secs = floor(secs);
  758.     t.tv_sec = (long)secs;
  759.     t.tv_usec = (long)(frac*1000000.0);
  760.     Py_BEGIN_ALLOW_THREADS
  761.     if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
  762. #ifdef EINTR
  763.         if (errno != EINTR) {
  764. #else
  765.         if (1) {
  766. #endif
  767.             Py_BLOCK_THREADS
  768.             PyErr_SetFromErrno(PyExc_IOError);
  769.             return -1;
  770.         }
  771.     }
  772.     Py_END_ALLOW_THREADS
  773. #else /* !HAVE_SELECT || __BEOS__ */
  774. #ifdef macintosh
  775. #define MacTicks    (* (long *)0x16A)
  776.     long deadline;
  777.     deadline = MacTicks + (long)(secs * 60.0);
  778.     while (MacTicks < deadline) {
  779.         /* XXX Should call some yielding function here */
  780.         if (PyErr_CheckSignals())
  781.             return -1;
  782.     }
  783. #else /* !macintosh */
  784. #if defined(__WATCOMC__) && !defined(__QNX__)
  785.     /* XXX Can't interrupt this sleep */
  786.     Py_BEGIN_ALLOW_THREADS
  787.     delay((int)(secs * 1000 + 0.5));  /* delay() uses milliseconds */
  788.     Py_END_ALLOW_THREADS
  789. #else /* !__WATCOMC__ || __QNX__ */
  790. #ifdef MSDOS
  791.     struct timeb t1, t2;
  792.     double frac;
  793.     extern double fmod Py_PROTO((double, double));
  794.     extern double floor Py_PROTO((double));
  795.     if (secs <= 0.0)
  796.         return;
  797.     frac = fmod(secs, 1.0);
  798.     secs = floor(secs);
  799.     ftime(&t1);
  800.     t2.time = t1.time + (int)secs;
  801.     t2.millitm = t1.millitm + (int)(frac*1000.0);
  802.     while (t2.millitm >= 1000) {
  803.         t2.time++;
  804.         t2.millitm -= 1000;
  805.     }
  806.     for (;;) {
  807. #ifdef QUICKWIN
  808.         Py_BEGIN_ALLOW_THREADS
  809.         _wyield();
  810.         Py_END_ALLOW_THREADS
  811. #endif
  812.         if (PyErr_CheckSignals())
  813.             return -1;
  814.         ftime(&t1);
  815.         if (t1.time > t2.time ||
  816.             t1.time == t2.time && t1.millitm >= t2.millitm)
  817.             break;
  818.     }
  819. #else /* !MSDOS */
  820. #ifdef MS_WIN32
  821.     /* XXX Can't interrupt this sleep */
  822.     Py_BEGIN_ALLOW_THREADS
  823.     Sleep((int)(secs*1000));
  824.     Py_END_ALLOW_THREADS
  825. #else /* !MS_WIN32 */
  826. #ifdef PYOS_OS2
  827.     /* This Sleep *IS* Interruptable by Exceptions */
  828.     Py_BEGIN_ALLOW_THREADS
  829.     if (DosSleep(secs * 1000) != NO_ERROR) {
  830.         Py_BLOCK_THREADS
  831.         PyErr_SetFromErrno(PyExc_IOError);
  832.         return -1;
  833.     }
  834.     Py_END_ALLOW_THREADS
  835. #else /* !PYOS_OS2 */
  836. #ifdef __BEOS__
  837.     /* This sleep *CAN BE* interrupted. */
  838.     {
  839.         if( secs <= 0.0 ) {
  840.             return;
  841.         }
  842.         
  843.         Py_BEGIN_ALLOW_THREADS
  844.         /* BeOS snooze() is in microseconds... */
  845.         if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
  846.             Py_BLOCK_THREADS
  847.             PyErr_SetFromErrno( PyExc_IOError );
  848.             return -1;
  849.         }
  850.         Py_END_ALLOW_THREADS
  851.     }
  852. #else /* !__BEOS__ */
  853. #ifdef _AMIGA
  854.     /* XXX Can't interrupt this sleep */
  855.     Py_BEGIN_ALLOW_THREADS
  856.     Delay((long)(secs*50));
  857.     Py_END_ALLOW_THREADS
  858. #else /* !_AMIGA */
  859.     /* XXX Can't interrupt this sleep */
  860.     Py_BEGIN_ALLOW_THREADS
  861.     sleep((int)secs);
  862.     Py_END_ALLOW_THREADS
  863. #endif /* !_AMIGA */
  864. #endif /* !__BEOS__ */
  865. #endif /* !PYOS_OS2 */
  866. #endif /* !MS_WIN32 */
  867. #endif /* !MSDOS */
  868. #endif /* !__WATCOMC__ || __QNX__ */
  869. #endif /* !macintosh */
  870. #endif /* !HAVE_SELECT */
  871.     return 0;
  872. }
  873.